﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Billing.Windows
{
    public partial class BulkOrderDetails : Window
    {
        BulkOrderBillingGrid bulkGrid;
        Color cursorEnterColor = Colors.Violet;
        Color cursorLeaveColor = Color.FromArgb(Convert.ToByte("FF", 16), Convert.ToByte("65", 16), Convert.ToByte("65", 16), Convert.ToByte("65", 16));
        double Total;

        public BulkOrderDetails(BulkOrderBillingGrid grid, string phone, DateTime dt, string desc, double totalAmt, double advance, int discountType, double discountByCost, double discountByPercentage, string reason, string customerName, string time, string place)
        {
            InitializeComponent();
            bulkGrid = grid;
            txtPhone.Focus();
            txtPhone.Text = phone;
            txtCustomer.Text = customerName;
            description.Text = desc;
            txtDeliveryPlace.Text = place;
            if (time != null)
            {
                int cntr = -1;
                foreach (ListBoxItem item in lstTime.Items)
                {
                    cntr++;
                    if (item.Content.ToString() == time)
                    {
                        lstTime.SelectedIndex = cntr;
                        lstTime.ScrollIntoView(lstTime.SelectedItem);
                        break;
                    }
                }
            }

            Total = totalAmt;
            txtAdvance.Text = advance.ToString();
            lblTotalAmt.Content = totalAmt.ToString();

            if (discountType == 1)
            {
                discount.IsReadOnly = false;
                discountReason.IsReadOnly = false;
                discount.Text = discountByCost.ToString();
                discountReason.Text = reason;
                discount.Text = discountByCost.ToString();
            }
            else if (discountType == 2)
            {
                discount.IsReadOnly = false;
                discountReason.IsReadOnly = false;
                discount.Text = discountByPercentage.ToString();
                discountReason.Text = reason;
                discount.Text = discountByPercentage.ToString();
            }

            int index = 0;

            for (int cnt = 0; cnt < 20; cnt++)
            {
                cmbDate.Items.Add(new ListBoxItem { Tag = DateTime.Now.AddDays(cnt), Content = DateTime.Now.AddDays(cnt).DayOfWeek.ToString() + "-" + DateTime.Now.AddDays(cnt).ToString("dd-MMM-yyyy") });

                if (DateTime.Now.AddDays(cnt).Day == dt.Day && DateTime.Now.AddDays(cnt).Month == dt.Month && DateTime.Now.AddDays(cnt).Year == dt.Year)
                {
                    index = cnt;
                }
            }

            cmbDate.SelectedIndex = index;
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Right)
                {
                    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
                    UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
                    if (elementWithFocus != null)
                        elementWithFocus.MoveFocus(request);
                }
                else if (e.Key == Key.Left)
                {
                    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Previous);
                    UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
                    if (elementWithFocus != null)
                        elementWithFocus.MoveFocus(request);
                }
                else if (e.Key == Key.F3 && discount.IsFocused)
                {
                    bulkGrid.DiscountType = 1;
                    discount.IsReadOnly = false;
                    discount.Clear();
                    discountReason.IsReadOnly = false;
                }
                else if (e.Key == Key.F4 && discount.IsFocused)
                {
                    bulkGrid.DiscountType = 2;
                    discount.IsReadOnly = false;
                    discount.Clear();
                    discountReason.IsReadOnly = false;
                }

                if (e.Key == Key.Escape || e.Key == Key.Enter)
                {
                    if (txtPhone.Text == string.Empty)
                    {
                        dphone.Foreground = new SolidColorBrush(Colors.Red);
                        return;
                    }

                    dphone.Foreground = new SolidColorBrush(Colors.White);
                    txtAdvance_LostFocus(sender, e);
                    discount_LostFocus(sender, e);

                    if (txtAdvance.Text.Trim() != string.Empty)
                    {
                        bulkGrid.PhoneNumberEntered = txtPhone.Text;
                        bulkGrid.DeliveryDateEntered = DateTime.Parse((cmbDate.SelectedItem as ListBoxItem).Tag.ToString());
                        bulkGrid.AdditionalDescEntered = description.Text;
                        bulkGrid.Advance = double.Parse(txtAdvance.Text);
                        bulkGrid.DiscountReason = discountReason.Text;
                        bulkGrid.CustomerName = txtCustomer.Text;
                        int time = 0;
                        if (lstTime.SelectedItem == null)
                        {
                            time = 7;
                            bulkGrid.DeliveryTime = "7 AM";
                        }
                        else
                        {
                            time = int.Parse((lstTime.SelectedItem as ListBoxItem).Tag.ToString());
                            bulkGrid.DeliveryTime = (lstTime.SelectedItem as ListBoxItem).Content.ToString();
                        }
                        bulkGrid.DeliveryPlace = txtDeliveryPlace.Text;

                        if (DateTime.Now.AddHours(2) > bulkGrid.DeliveryDateEntered.Date.AddHours(time))
                        {
                            brdrDate.Background = new SolidColorBrush(Colors.Red);
                            dd.Foreground = new SolidColorBrush(Colors.Red);
                        }
                        else
                        {
                            brdrDate.Background = new SolidColorBrush(cursorLeaveColor);
                            dd.Foreground = new SolidColorBrush(Colors.White);
                            this.Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void txt_GotFocus(object sender, RoutedEventArgs e)
        {
            ((sender as TextBox).Parent as Border).Background = new SolidColorBrush(cursorEnterColor);
        }

        private void txt_LostFocus(object sender, RoutedEventArgs e)
        {
            ((sender as TextBox).Parent as Border).Background = new SolidColorBrush(cursorLeaveColor);
        }

        private void dt_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            ((sender as ComboBox).Parent as Border).Background = new SolidColorBrush(cursorEnterColor);
        }

        private void dt_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            ((sender as ComboBox).Parent as Border).Background = new SolidColorBrush(cursorLeaveColor);
        }

        private void txtAdvance_LostFocus(object sender, RoutedEventArgs e)
        {
            double advance;
            if (!(double.TryParse(txtAdvance.Text, out advance)))
            {
                txtAdvance.Clear();
            }
            else
            {
                if (advance > Total)
                {
                    txtAdvance.Clear();
                }
                else
                {
                    (txtAdvance.Parent as Border).Background = new SolidColorBrush(cursorLeaveColor);
                }
            }
        }

        private void discount_LostFocus(object sender, RoutedEventArgs e)
        {
            if (discount.Text != string.Empty)
            {
                double disc;
                if (!(double.TryParse(discount.Text, out disc)))
                {
                    discount.Clear();
                    return;
                }
                else if ((bulkGrid.DiscountType == 1 && disc > Total) || (bulkGrid.DiscountType == 2 && disc >= 100))
                {
                    discount.Clear();
                    return;
                }

                if (bulkGrid.DiscountType == 1)
                    bulkGrid.DiscountByCost = disc;
                else if (bulkGrid.DiscountType == 2)
                    bulkGrid.DiscountByPercentage = disc;
            }

            (discount.Parent as Border).Background = new SolidColorBrush(cursorLeaveColor);
        }

        private void cmbDate_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Right || e.Key == Key.Left)
                e.Handled = true;
            else
                e.Handled = false;
        }
    }
}
